package com.sdy.hospitalguard.web.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdy.hospitalguard.biz.event.EventPublisher;
import com.sdy.hospitalguard.biz.event.WebsocketEvent;
import com.sdy.hospitalguard.biz.event.WebsocketEventCallback;
import com.sdy.hospitalguard.biz.model.Manager;
import com.sdy.hospitalguard.biz.model.VisitTimeout;
import com.sdy.hospitalguard.biz.service.ManagerService;
import com.sdy.hospitalguard.biz.service.TemporaryCardService;
import com.sdy.hospitalguard.biz.service.VisitTimeoutService;
import com.sdy.mvc.annotation.TaskSync;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * <p>
 * 注释
 * </p>
 *
 * @author fb
 * @since 2020/6/1
 */
@Slf4j
@Component
public class VisitTimeoutTask {

    @Autowired
    private EventPublisher eventPublisher;
    @Autowired
    private VisitTimeoutService visitTimeoutService;
    @Autowired
    private ManagerService managerService;
    @Autowired
    private TemporaryCardService temporaryCardService;

    /**
     * 产生探视超时记录，实时性要求高
     */
    @Scheduled(cron = "2/5 * * * * ?")
    @TaskSync
    public void timeout() {
        visitTimeoutService.saveTimeout();
    }

    /**
     * 超时告警，实时性不需很高
     * @throws Exception
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    @TaskSync
    public void sendMessage() throws Exception{
        // 获取所有护士站的用户
        List<Manager> managerList = managerService.list(new QueryWrapper<Manager>().eq("role_id", 3).orderByAsc("id"));
        Integer managerSize = managerList.size();
        for (int i=0; i<managerSize; i++) {
            Manager manager = managerList.get(i);
            Integer areaId = manager.getInpatientAreaId();
            List<VisitTimeout> list = visitTimeoutService.list(new QueryWrapper<VisitTimeout>()
                    .eq("status", 0)
                    .eq("area_id", areaId));
            if (list.size() > 0) {
                WebsocketEvent event = new WebsocketEvent(getClass().getName(), manager.getId(), "0", list, callback.class);
                eventPublisher.publishWebsocketEvent(event);
            }
        }
    }

    public static class callback extends WebsocketEventCallback {

        @Override
        public void onError(ApplicationContext applicationContext, WebsocketEvent event) {
            log.warn("警告,出现失败,用户id={}", event.getUserId());
        }

        @Override
        public void onSuccess(ApplicationContext applicationContext, WebsocketEvent event) {
            List<VisitTimeout> list = (List<VisitTimeout>)event.getData();
            VisitTimeoutService visitTimeoutService = applicationContext.getBean(VisitTimeoutService.class);
            for (int i=0; i<list.size(); i++) {
                visitTimeoutService.update(new UpdateWrapper<VisitTimeout>()
                        .eq("id", list.get(i).getId())
                            .set("status", 1));
            }
            log.info("成功,用户id={}", event.getUserId());
        }
    }

    /**
     * 临时卡失效，实时性要求高
     * @throws Exception
     */
    @Scheduled(cron = "0/5 * * * * ?")
    @TaskSync
    public void cardFailure() throws Exception{
        temporaryCardService.cardFailure();
    }

}
